<?php

namespace App\Listeners\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\UserContractEnum;
use App\Events\OpenShop\UserContractCreatedEvent;
use App\Listeners\BaseListener;
use App\Models\OpenShop\InvestmentPromotion\OpenShopContractTemplateModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderLogModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserContractModel;
use App\Remote\Rms\RmsUserRemote;
use App\Service\OpenShop\User\RmsUserService;
use App\Support\ChangeSupport;

class OrderContractCreatedLogListener extends BaseListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Handle the event.
     *
     * @param UserContractCreatedEvent $event
     * @return void
     */
    public function handle(UserContractCreatedEvent $event)
    {
        $contract = OpenShopUserContractModel::find($event->contractId);
        if (null === $contract) {
            return;
        }
        $templateName = (string)OpenShopContractTemplateModel::query()
            ->where('id', $contract->policyTemplateId)
            ->value('templateName');
        if (!strlen($templateName)) {
            $templateName = "ID为{$contract->ppolicyTemplateId}";
        }
        $orderId = OpenShopSignOrderModel::query()
            ->where('orderNo', $contract->orderNo)
            ->value('id');
        if (null === $orderId) {
            return;
        }

        $body = RmsUserService::getRmsUserInfoByUserIds([$event->rmsUserId]);
        $operatorName = $body['data'][0]['username'] ?? "userId:{$event->rmsUserId}";

        ChangeSupport::getInstance()->buildInsertChanges(
            "合同创建",
            $contract->toArray(),
            OpenShopUserContractModel::getOrderLogTranslate()
        );

        ChangeSupport::getInstance()->batchChangesWriteToOrderLog(
            $orderId,
            OpenShopSignOrderLogModel::SEND_ORDER_CONTRACT_CREATED,
            $operatorName
        );

//        $body = RmsUserService::getRmsUserInfoByUserIds([$event->rmsUserId]);
//        $operatorName = $body['data'][0]['username'] ?? "userId:{$event->rmsUserId}";
//
//        $changes = "✍️创建订单合同\n";
//        $changes .= "第三方合同编号：{$contract->contractExtNo}" . PHP_EOL;
//        $changes .= "签约主题：" . (UserContractEnum::CONTRACTORS[$contract->contractors] ?? '-') . PHP_EOL;
//        $changes .= "公司名称：{$contract->companyName}\n";
//        $changes .= "营业执照号：{$contract->businessLicenseNumber}\n";
//        $changes .= "合同开始时间：" . date('Y-m-d', $contract->startTime) . "\n";
//        $changes .= "合同截止时间：" . date('Y-m-d', $contract->endTime) . "\n";
//        $changes .= "签约类型：" . (UserContractEnum::CONTRACT_TYPE[$contract->contractType] ?? '-') . "\n";
//        $changes .= "政策模版：{$templateName}\n";
//        $changes .= "内部备注：{$contract->remark}\n";
//        $changes .= "合同年限：{$contract->year}\n";
//        $changes .= "加盟费：" . ($contract->initialFee / 100) . "\n";
//        $changes .= "技术转让费：" . ($contract->technologyTransferFee / 100) . "\n";
//        $changes .= "保证金：" . ($contract->deposit / 100) . "\n";
//        $changes .= "特许权使用费第一年：" . ($contract->managementCost / 100) . "\n";
//        $changes .= "装修保证金：" . ($contract->decorationDeposit / 100) . "\n";
//        $changes .= "店铺数量：{$contract->shopCount}\n";
//
//        $log = new OpenShopSignOrderLogModel;
//        $log->orderId = $orderId;
//        $log->content = '创建订单合同';
//        $log->type = OpenShopSignOrderLogModel::SEND_ORDER_CONTRACT_CREATED;
//        $log->operatorName = $operatorName;
//        $log->createdByUid = $event->rmsUserId;
//        $log->updatedByUid = $event->rmsUserId;
//        $log->changes = $changes;
//        $log->save();
    }
}
